/**
 * 北京中流科技有限公司 版权所有
 * http://www.chinasoa.com.cn
 */
package cn.com.chinasoa.course.subscribe;

import java.io.IOException;

import javax.persistence.EntityManager;
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletException;

import cn.com.chinasoa.course.subscribe.model.SubscribeOrder;
import cn.com.chinasoa.course.subscribe.util.DataTypeUtil;
import cn.com.chinasoa.course.subscribe.util.EntityManagerHelper;

import com.x4ee.soa.portal.framework.core.ActionConfig;
import com.x4ee.soa.portal.framework.core.ActionFilter;
import com.x4ee.soa.portal.framework.core.BaseRequestFilter;

/**
 * @author 刘洋(liuyang@chinasoa.com.cn)
 * 2007-11-5上午02:10:23
 */
public class SubscribeOrderOperateActionFilter extends BaseRequestFilter
	implements ActionFilter {
	
	private static String SUCCESS = "success";
	private static String FAILED = "error";
	
	public String process(ActionRequest actionRequest, ActionResponse actionResponse,
			ActionConfig actionConfig) throws PortletException, IOException {
		String orderId = actionRequest.getParameter("orderId");
		String cmd = actionRequest.getParameter("cmd");

		boolean success = false;
		if((orderId!=null)&&(cmd!=null)){
			long orderIdLong = DataTypeUtil.parseLong(orderId);
			if(orderIdLong>=0){
				EntityManager em = EntityManagerHelper.getEntityManager();
				em.getTransaction().begin();
				SubscribeOrder tSubscribeOrder = em.find(SubscribeOrder.class,orderIdLong);
				if(tSubscribeOrder!=null){
					if(cmd.equals("delete")){
						em.remove(tSubscribeOrder);
						success=true;
					}else{
						if(cmd.equals("confirm")){
							tSubscribeOrder.setStatus(SubscribeOrder.HAVE_PAYED);
							success=true;
						}else{
							if(cmd.equals("finish")){
								tSubscribeOrder.setStatus(SubscribeOrder.PASSED);
								success=true;
							}
						}
					}
				}
				em.getTransaction().commit();
				EntityManagerHelper.closeEntityManager();
			}
		}
		if(success)return SUCCESS;
		else return FAILED;
	}
}
